//
//  BCImagePickerViewController.m
//  GSBlockChain
//
//  Created by guoshikeji_a on 2018/11/28.
//  Copyright © 2018 guoshikeji. All rights reserved.
//

#import "BCImagePickerViewController.h"

//
#import "BCImageresizerController.h"

@interface BCImagePickerViewController () <QMUIImagePickerViewControllerDelegate,BCImageresizerControllerDelegate>

@end

@implementation BCImagePickerViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    //设置代理为自己
    self.imagePickerViewControllerDelegate = self;
    //
    self.collectionViewLayout.sectionInset = UIEdgeInsetsMake(Height_NavBar, 0, Height_Bottom_SafeArea, 0);
    self.collectionView.alwaysBounceVertical = true;
    //
    self.allowsMultipleSelection = false;
    //

}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    //
    [self.navigationController setNavigationBarHidden:false animated:true];
    
    //
    [QMUIImagePickerHelper updateLastestAlbumWithAssetsGroup:self.assetsGroup ablumContentType:QMUIAlbumContentTypeOnlyPhoto userIdentify:nil];
    
    //
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        CGSize collectionViewContentSize = self.collectionView.collectionViewLayout.collectionViewContentSize;
        CGSize collectionViewSize = self.collectionView.frame.size;
        if (collectionViewContentSize.height>collectionViewSize.height) {
            self.collectionView.contentOffset = CGPointMake(0, collectionViewContentSize.height-collectionViewSize.height);
        }
    });
}

- (void)setupNavigationItems {
    [super setupNavigationItems];
    //
    if (self.navigationItem.rightBarButtonItem) {
        SEL action = self.navigationItem.rightBarButtonItem.action;
        id target = self.navigationItem.rightBarButtonItem.target;
        self.navigationItem.rightBarButtonItem = [UIBarButtonItem qmui_itemWithTitle:BCLocalizedKey(@"general_cancel") target:target action:action];
    }
}



#pragma mark - QMUIImagePickerViewControllerDelegate
/// 是否能够选中 checkbox
- (BOOL)imagePickerViewController:(QMUIImagePickerViewController *)imagePickerViewController shouldCheckImageAtIndex:(NSInteger)index {
    return false;
}
/**
 *  cell 被点击时调用（先调用这个接口，然后才去走预览大图的逻辑），注意这并非指选中 checkbox 事件
 *
 *  @param imagePickerViewController        对应的 QMUIImagePickerViewController
 *  @param imageAsset                       被选中的图片的 QMUIAsset 对象
 *  @param imagePickerPreviewViewController 选中图片后进行图片预览的 viewController
 */
- (void)imagePickerViewController:(QMUIImagePickerViewController *)imagePickerViewController didSelectImageWithImagesAsset:(QMUIAsset *)imageAsset afterImagePickerPreviewViewControllerUpdate:(QMUIImagePickerPreviewViewController *)imagePickerPreviewViewController {
    UIImage *originalImage = [imageAsset originImage];
    if (originalImage) {
        [self toImageresizerWithImage:originalImage];
    }
}

/**
 *  取消选择图片后被调用
 */
- (void)imagePickerViewControllerDidCancel:(QMUIImagePickerViewController *)imagePickerViewController {
    
}




#pragma mark - actions
//去裁减图片
- (void)toImageresizerWithImage:(UIImage *)image {
    BCImageresizerController *imz = [BCImageresizerController new];
    imz.image = image;
    imz.delegate = self;
    [self.navigationController pushViewController:imz animated:true];
}

#pragma mark - BCImageresizerControllerDelegate
- (void)imageresizerControllerDidResizeredWithImage:(UIImage *)resierImage {
    if (self.chooseDelegate && [self.chooseDelegate respondsToSelector:@selector(bc_imagePickerViewControllerComplete:)]) {
        [self.chooseDelegate bc_imagePickerViewControllerComplete:resierImage];
    }
}

@end
